<?php

/**
 *身形数据测试
 * @author cjli
 *
 */
class MeasureAction extends HomeAction
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function cart()
    {
        global $user;
        $cid = intval($_REQUEST['cid']);
        if ($cid) {
            session('custom_id', $cid);
        }

        if (!UserModel::logged()) {
            redirect(url('user/login', array('src' => base64_encode(url('measure/cart')))));
        }

        $user_measure = D('user_measure');
        $mid = intval($_REQUEST['mid']);

        if ($mid) {
            $measureInfo = $user_measure->find($mid);
            if (!$measureInfo) {
                //$_SESSION['custom_id'] = $cid;
                redirect(url('measure/add'));
            } else {
                $measure_cookie = array(
                    'user_id' => $user['id'],
                    'measure_data' => unserialize($measureInfo['measure_data']),
                );
                cookie('measure', base64_encode(serialize($measure_cookie)));

                $cart_db = D('cart_products');
                $data['measure_id'] = $mid;
                $cart_db->where('id=' . $cid)->save($data);
                //redirect(url('cart/checkout'));
                redirect(url('cart/info'));
                exit;
            }
        }
        $measureList = $user_measure->where('user_id=' . $user['id'])->select();
        if (empty($measureList)) {
            //$_SESSION['custom_id'] = $cid;
            session('custom_id', $cid);
            redirect(url('measure/add'));
        } else {
            if (!$cid) {
                $cid = $_SESSION['custom_id'];
            }
            $this->assign('cid', $cid);
            $this->assign('measureList', $measureList);
            $this->display('cart');
        }
    }

    public function camera_ajax()
    {
        if (isset($_POST['image'][0]) && isset($_POST['image'][1])) {
            $image_1 = substr($_POST['image'][0], 22);
            $image_2 = substr($_POST['image'][1], 22);
            $front_img = MeasureModel::file_put_images($image_1);
            $side_img = MeasureModel::file_put_images($image_2);
            cookie('measure_front_img', $front_img, 1800);
            cookie('measure_side_img', $side_img, 1800);
        }
    }

    public function camera()
    {
//            $image_1 = substr($_POST['image'][0],22);
//            $image_2 = substr($_POST['image'][1],22);
//            $front_img = MeasureModel::file_put_images($image_1);
//            $side_img = MeasureModel::file_put_images($image_2);
        //var_dump($_FILES);DIE;
//            json_encode($_FILES);die;
        global $user;
        $cookie_data = unserialize(base64_decode($_COOKIE['measure_data_tmp']));
        $type = $cookie_data['type'];
        if (!$type) {
            $type = $_REQUEST['type'];
        }
        if ($type == 'user') {
            $back_url = url('/measure/edit', array('type' => 'user', 'mid' => $_GET['mid']));
        } else if ($type == 'cart') {
            $back_url = url('cart/info');
        }
        $db = M('user_measure');
        $mid = intval($_REQUEST['mid']);

        if ($mid) {
            if ($user['id']) {
                $where['user_id'] = $user['id'];
            } else if ($user['sid']) {
                $where['sid'] = $user['sid'];
            }
            $where['id'] = $mid;
            $measure_Info = $db->where($where)->find();
            $back_url = url('/measure/edit', array('type' => 'user', 'mid' => $_GET['mid']));
            if (!$measure_Info) {
                $this->error(L('_ERROR_ACTION_'), url('user/user_measure'));
            }
        }


//                if(isset($_FILES['front_img']) ||  $_FILES['side_img']){
//                    $info = $this->upload(date('Y/m/d', time()));
//                }
        if ($mid) {
            $m_data = array(
                'front_img' => $_COOKIE['measure_front_img'],
                'side_img' => $_COOKIE['measure_side_img']
            );
            if ($measure_Info['m_status'] == 1) {
                $m_data['m_status'] = 4;
            }

            if ($measure_Info['m_status'] == 2) {
                $m_data['m_status'] = 3;
            }

            $result = $db->where('id=' . $mid)->save($m_data);
            setcookie('measure_front_img', null);
            setcookie('measure_side_img', null);
        } else {

            $data = array(
                'user_id' => isset($user['id']) ? intval($user['id']) : 0,
                'sid' => $user['sid'],
                'age' => $cookie_data['measure']['age'],
                'measure_data' => serialize($cookie_data['measure']),
                'height' => $cookie_data['measure']['height']['cm'],
                'weight' => $cookie_data['measure']['weight']['kg'],
                'measure_name' => $cookie_data['measure_name'],
                'update_time' => time(),
                'm_status' => 3,
                'clothes_size' => $cookie_data['clothes_size'],
                'pants_size' => $cookie_data['pants_size'],
                'm_unit' => $cookie_data['m_unit'],
                'front_img' => $_COOKIE['measure_front_img'],
                'side_img' => $_COOKIE['measure_side_img']
            );
            $result = $db->add($data);
            setcookie('measure_front_img', null);
            setcookie('measure_side_img', null);
            //cookie('measure_data_tmp',null);
        }

        if ($result) {
            $this->success(L('measure_data_save_success'), $back_url);
            exit;
        } else {
            $this->error(L('measure_data_save_falure'), $back_url);
            exit;
        }
    }

    public function uncode_camera()
    {
        $info = unserialize(file_get_contents('234.txt'));
        $image_1 = substr($info['image'][0], 22);
        $image_2 = substr($info['image'][1], 22);
        $front_img = MeasureModel::file_put_images($image_1);
        $side_img = MeasureModel::file_put_images($image_2);
    }

    public function edit()
    {
        global $user;
        $pid=intval($_GET['pid']);
        $measure_desc = array(//体型语言包参数
            'shoulders' => array(
                0 => 'square_shoulders',
                1 => 'square_shoulders',
                2 => 'between_square_and_rounder',
                3 => 'rounder_shoulders',
                4 => 'between_rounder_and_sloping',
                5 => 'sloping_shoulders',
            ),
            'chest' => array(
                0 => 'muscular_chest',
                1 => 'muscular_chest',
                2 => 'between_muscular_and_flat',
                3 => 'flat_chest',
                4 => 'between_flat_and_large',
                5 => 'large_chest',
            ),
            'stomach' => array(
                0 => 'flat_stomach ',
                1 => 'flat_stomach ',
                2 => 'between_flat_and_slightly_rounded ',
                3 => 'slightly_rounded_stomach',
                4 => 'between_slightly_rounded _and_rounded',
                5 => 'rounded_stomach',
            ),
            'posture' => array(
                0 => 'flat_posture',
                1 => 'flat_posture',
                2 => 'between_flat_and_straight',
                3 => 'straight_posture',
                4 => 'between_straight_and_relaxed',
                5 => 'relaxed_posture',
            )
        );
        $mid = intval($_REQUEST['mid']);
        $user_measure_db = M('user_measure');
        $where = array(
            'id' => $mid,
        );
        if (UserModel::logged()) {
            $where['user_id'] = $user['id'];
        } else {
            $where['sid'] = $user['sid'];
        }
        //var_dump($where);exit;
        $measureInfo = $user_measure_db->where($where)->find();
        if (!$measureInfo) {
            if (UserModel::logged()) {
                $this->error(L('_ERROR_ACTION_'), url('user/user_measure'));
            } else {
                $this->error(L('_ERROR_ACTION_'), url('user/login'));
            }
            exit;
        }
        if (IS_POST) {
            $measure_data = unserialize($measureInfo['measure_data']);
            $measure_data2 = unserialize($measureInfo['measure_data2']);
            $measure_data['weight']['kg'] = $_POST['measure']['weight'];
            $measure_data['weight']['lb'] = number_format($_POST['measure']['weight'] * 2.2, 2);
            $measure_data['height']['cm'] = $_POST['measure']['height'];
            $measure_data['height']['ft'] = (string)intval($_POST['measure']['height'] / 30.48);
            $measure_data['height']['in'] = number_format((($_POST['measure']['height'] / 30.48 - $measure_data['height']['ft']) * 12), 2);
            $measure_data['age'] = $_POST['measure']['age'];
            $measure_data2['weight']['kg'] = $_POST['measure']['weight'];
            //量体数据开始
            if(isset($_POST['measure']['neck'])){
                $measure_data['neck']['cm'] = $_POST['measure']['neck'];
            }
            if(isset($_POST['measure']['jacket_back'])){
                $measure_data['jacket_back']['cm'] = $_POST['measure']['jacket_back'];
            }
            if(isset($_POST['measure']['bicep'])){
                $measure_data['bicep']['cm'] = $_POST['measure']['bicep'];
            }
            if(isset($_POST['measure']['sleeve'])){
                $measure_data['sleeve']['cm'] = $_POST['measure']['sleeve'];
            }
            if(isset($_POST['measure']['wrist'])){
                $measure_data['wrist']['cm'] = $_POST['measure']['wrist'];
            }
            if(isset($_POST['measure']['shirt'])){
                $measure_data['shirt']['cm'] = $_POST['measure']['shirt'];
            }
            if(isset($_POST['measure']['chest'])){
                $measure_data['chest']['cm'] = $_POST['measure']['chest'];
            }
            if(isset($_POST['measure']['stomach'])){
                $measure_data['stomach']['cm'] = $_POST['measure']['stomach'];
            }
            if(isset($_POST['measure']['waist'])){
                $measure_data['waist']['cm'] = $_POST['measure']['waist'];
            }
            if(isset($_POST['measure']['pants_hips'])){
                $measure_data['pants_hips']['cm'] = $_POST['measure']['pants_hips'];
            }
            if(isset($_POST['measure']['pants'])){
                $measure_data['pants']['cm'] = $_POST['measure']['pants'];
            }
            if(isset($_POST['measure']['crotch'])){
                $measure_data['crotch']['cm'] = $_POST['measure']['crotch'];
            }
            if(isset($_POST['measure']['thigh'])){
                $measure_data['thigh']['cm'] = $_POST['measure']['thigh'];
            }
            if(isset($_POST['measure']['knee'])){
                $measure_data['knee']['cm'] = $_POST['measure']['knee'];
            }
            //量体数据结束
            $data = array(
                'measure_data' => serialize($measure_data),
                'measure_data2' => serialize($measure_data2),
                'measure_name' => $_POST['measure_name'],
                'weight' => $_POST['measure']['weight'],
                'height' => $_POST['measure']['height'],
                'age' => $_POST['measure']['age'],
                'clothes_size' => $_POST['clothes_size'],
                'pants_size' => $_POST['pants_size']
            );
            //量体数据开始
            if(isset($_POST['measure']['neck'])){
                $data['neck']= $_POST['measure']['neck'];
            }
            if(isset($_POST['measure']['jacket_back'])){
                $data['jacket_back']= $_POST['measure']['jacket_back'];
            }
            if(isset($_POST['measure']['bicep'])){
                $data['bicep']= $_POST['measure']['bicep'];
            }
            if(isset($_POST['measure']['sleeve'])){
                $data['sleeve']= $_POST['measure']['sleeve'];
            }
            if(isset($_POST['measure']['wrist'])){
                $data['wrist']= $_POST['measure']['wrist'];
            }
            if(isset($_POST['measure']['shirt'])){
                $data['shirt']= $_POST['measure']['shirt'];
            }
            if(isset($_POST['measure']['chest'])){
                $data['chest']= $_POST['measure']['chest'];
            }
            if(isset($_POST['measure']['stomach'])){
                $data['stomach']= $_POST['measure']['stomach'];
            }
            if(isset($_POST['measure']['waist'])){
                $data['waist']= $_POST['measure']['waist'];
            }
            if(isset($_POST['measure']['pants_hips'])){
                $data['pants_hips']= $_POST['measure']['pants_hips'];
            }
            if(isset($_POST['measure']['pants'])){
                $data['pants']= $_POST['measure']['pants'];
            }
            if(isset($_POST['measure']['crotch'])){
                $data['crotch']= $_POST['measure']['crotch'];
            }
            if(isset($_POST['measure']['thigh'])){
                $data['thigh']= $_POST['measure']['thigh'];
            }
            if(isset($_POST['measure']['knee'])){
                $data['knee']= $_POST['measure']['knee'];
            }
            //量体数据结束
            $user_measure_db->where('id=' . intval($_REQUEST['mid']))->save($data);
//                if($_GET['type'] == 'cart'){
//                    $this->redirect('cart/info');
//                    exit;
//                }
            if ($_GET['type'] == 'cartshow') {
                $this->redirect(url('cart/info'));
                exit;
            }
            $this->redirect('user/user_measure');
            die;
        } else if ($_GET['change'] == 'unit') {
            if ($measureInfo['m_unit'] == 1) {
                $unit = 2;
            } else if ($measureInfo['m_unit'] == 2) {
                $unit = 1;
            }
            M('user_measure')->query('update user_measure set m_unit=' . $unit . ' where id=' . $mid);
            $this->redirect('/measure/edit/type/user/mid/' . $mid);
            die;
        }
        $measure_field = MeasureModel::user_measure_field();
        //$measure = unserialize($measureInfo['measure_data']);
        //*******************
        //获取身型字段 兼容原先代码

        $measure_field = MeasureModel::user_measure_field();
        foreach ($measure_field as $key => $val) {
            $value = $measureInfo[$val['measure_field']];
            if ($val['measure_field'] == 'age') {
                $new_measure_data[$val['measure_field']] = $value;
            } else {
                $new_measure_data[$val['measure_field']][$val['field_type']] = $value;
            }
        }
        $m_unit = $measureInfo['m_unit'];
        foreach ($new_measure_data as $k => $v) {


            //in to cm 2.54    1 inch = 2.54 cm
            //ft to cm 30.48   1 foot = 30.48 cm
            //ft to in 12      1 foot = 12 inches
            //例 180 = 5 ft 10.87 in    算法 int(180 / 30.48) ft ((180/30.48)-int(180 / 30.48))*12 in  == 5 ft  10.87 in

            if ($k == 'height') {
                continue;
            }
            if (isset($new_measure_data[$k]['cm']) && $m_unit == 1) { //换算单位
                $new_measure_data[$k]['size'] = $new_measure_data[$k]['cm'] . 'cm ';
            } else if (isset($new_measure_data[$k]['cm']) && $m_unit == 2) {
                $ft = intval($new_measure_data[$k]['cm'] / 30.48);
                $in = number_format((($new_measure_data[$k]['cm'] / 30.48 - $ft) * 12), 2);
                $new_measure_data[$k]['size'] = $ft . 'ft ' . $in . 'in ';
            }
        }
        $new_measure_data['measure_name'] = $measureInfo['measure_name'];
        $weight = $measureInfo['weight'];
        $height = $measureInfo['height'];
        $clothes_size_list = $this->get_size_value_ajax($height, $weight, false);
        if ($clothes_size_list) {
            $this->assign('clothes_size_list', $clothes_size_list);
        }
        //量体优化开始
        $product_info=ProductModel::get_product_info($pid);
        $model_id=$product_info['model_id'];
        $measure_data_model=C("data_".$model_id);
        $this->assign('measure_data_model', $measure_data_model);//需要的身形数据
        if(!$pid){
            //判断购物车商品
            $data_arr=array();
            $cartList =  CartModel::get_cart_product_list();
            if($cartList){
                foreach ($cartList['productList'] as &$cart){
                    // 获取商品信息
                    $productInfo = ProductModel::get_product_info($cart['pid']);
                    $model_id=$productInfo['model_id'];
                    if(C("data_".$model_id)){
                        foreach (C("data_".$model_id) as $v){
                            $data_arr[]=$v;
                        }
                    }

                }

            }
            if($cartList['productList']){
                $cart=1;
            }else{
                $cart=0;
            }
            $this->assign('cart',$cart);
        }
        $data_arr= array_unique($data_arr);
        $this->assign('data_arr',$data_arr);
        //print_r($data_arr);
        //量体优化结束
        $this->assign('measure_desc', $measure_desc);//形体语言包参数
        $this->assign('measureInfo', $measureInfo);
        $this->assign('m_unit', $m_unit);//使用厘米单位还是英寸单位
        $this->assign('measure_data', $new_measure_data);
        $this->assign('type', $_GET['type']);
        $this->display('edit');

    }

    public function add()
    {
        global $user;
        $data = array(
            'docTitle' => L('meta_title_add'),
            'docKeywords' => L('meta_keywords'),
            'docDescription' => L('meta_description'),
        );
        $this->assign($data);

        $cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : 0;

        $type = $_REQUEST['type'];
        if ($type == 'user') {
            $gourl = url('measure/add', array('type' => 'user'));
            if (isset($_GET['in']) || isset($_GET['get'])) {
                $backurl = url('measure/edit', array('type' => 'user', 'mid' => $_GET['mid']));
            } else {
                $backurl = url('user/user_measure');
            }
        } else if ($type == 'cart') {
            $gourl_arr['type'] = 'cart';
            if ($cid > 0) $gourl_arr['cid'] = $cid;
            if (isset($_REQUEST['set']) && $_REQUEST['set'] = 'all') {
                $gourl_arr['set'] = 'all';
                //如果已登陆的用户且有身型数据， 是默认有身型数据的
                if (UserModel::logged() && AttributeModel::get_user_measure_mid()) {
                    redirect(url('cart/info'));
                    exit;
                }
            }
            $backurl = url('cart/info');
        } else if ($type == 'goto') {
            $gourl = url('measure/add', array('type' => 'goto', 'jump_src' => $_GET['jump_src']));
            $jump_src = base64_decode($_GET['jump_src']);
            $backurl = url($jump_src);
        } else if ($type == 'cartshow') {
            $gourl = url('measure/edit', array('type' => 'cartshow', 'mid' => $_GET['mid']));
            $backurl = url('measure/edit', array('type' => 'cartshow', 'mid' => $_GET['mid']));
        } else {
            //$backurl = url('cart/checkout');
            $backurl = url('cart/info');
        }

        if (!UserModel::logged() && !$_GET['countiue_buy']) {
            cookie("action_measure","measure");
            redirect(url('user/login', array('src' => base64_encode($gourl),'action'=>'measure')));
            exit;
        }
        if ($_GET['countiue_buy']) {
            $backurl = url('cart/info');
        }
        $user_measure = D('user_measure');

        $mid = intval($_REQUEST['mid']);
        if ($mid) {
            $measureInfo = $user_measure->find($mid);
            if (!$measureInfo || $measureInfo['user_id'] != $user['id']) {
                $this->error(L('_ERROR_ACTION_'), url('user/user_measure'));
            }
            //$measure = unserialize($measureInfo['measure_data']);
            //*******************
            //获取身型字段 兼容原先代码

            //in to cm 2.54    1 inch = 2.54 cm
            //ft to cm 30.48   1 foot = 30.48 cm
            //ft to in 12      1 foot = 12 inches
            //例 180 = 5 ft 10.87 in    算法 int(180 / 30.48) ft ((180/30.48)-int(180 / 30.48))*12 in  == 5 ft  10.87 in
//               $measure_data['weight']['lb'] = number_format($_POST['measure']['weight'] * 2.2,2);
//                $measure_data['height']['cm'] = $_POST['measure']['height'];
//                $measure_data['height']['ft'] = (string)intval($_POST['measure']['height'] / 30.48);
//                $measure_data['height']['in'] = number_format((($_POST['measure']['height'] / 30.48 - $measure_data['height']['ft']) * 12),2);
            $measure_field = MeasureModel::user_measure_field();

            foreach ($measure_field as $key => $val) {
                $value = $measureInfo[$val['measure_field']];
                if ($value == 0) {
                    continue;
                }
                if ($val['measure_field'] == 'age') {
                    $new_measure_data[$val['measure_field']] = $value;
                } else if ($val['measure_field'] == 'chest_type') {
                    $new_measure_data['chest'][$val['field_type']] = $value;
                } else if ($val['measure_field'] == 'stomach_type') {
                    $new_measure_data['stomach'][$val['field_type']] = $value;
                } else {
                    $new_measure_data[$val['measure_field']][$val['field_type']] = $value;
                }
                if ($val['field_type'] == 'cm') {
                    $new_measure_data[$val['measure_field']]['ft'] = (string)intval($value / 30.48);
                    $new_measure_data[$val['measure_field']]['in'] = number_format((($value / 30.48 - $new_measure_data[$val['measure_field']]['ft']) * 12), 2);
                }
                if ($val['field_type'] == 'kg') {
                    $new_measure_data[$val['measure_field']]['lb'] = number_format($value * 2.2, 2);
                }
            }
            $measure = $new_measure_data;
            if ($measureInfo['m_status'] == 2 || $measureInfo['m_status'] == 3) {// 2第一次选择身形方式选择了免量体或拍照
                $backurl = url('measure/edit', array('type' => 'user', 'mid' => $_GET['mid']));
            }
            $this->assign('m_unit', $measureInfo['m_unit']);
            //********************
        } else {
            $this->assign('m_unit', 2);
        }
        //var_dump($measureInfo['m_unit']);die;
        //var_dump($measure);die;
        //dump($_POST);exit;
        if ($_POST['dosubmit']) {
            //var_dump($_POST);die;
            if (empty($_POST['measure']) || !is_array($_POST['measure'])) {
                $this->error(L('measure data empty'), url('user/user_measure'));
            }
            if ($type != 'user') {
                $measure_cookie = array(
                    'user_id' => $user['id'],
                    'measure_data' => $_POST['measure'],
                );
                cookie('measure', base64_encode(serialize($measure_cookie)));
            }


            if (!$mid) {
                $mid = MeasureModel::add_measure_data($_POST);
            } else {
                MeasureModel::edit_measure_data($_POST, $mid);
            }
            if ($mid) {
                if ($type == 'cart') {
                    $attribute_list = AttributeModel::get_user_measure_list();
                    if ($cid == 0 || (count($attribute_list) == 1)) {
                        if (isset($user['id'])) {
                            $cart_db_where['user_id'] = $user['id'];
                        } else {
                            $cart_db_where['sid'] = $user['sid'];
                        }
                        $cart_db_where['custom_type'] = 2;
                        $cart_db_where['measure_id'] = array('eq', 0);
                    } else {
                        $cart_db_where = 'id=' . $cid;
                    }
                    $cart_db = D('cart_products');
                    $data['measure_id'] = $mid;
                    $cart_db->where($cart_db_where)->save($data);
                    if (isset($_REQUEST['set']) && $_REQUEST['set'] = 'all') $backurl = url('cart/checkout');
                } else if ($type == 'user') {
                    $backurl = url('measure/edit', array('type' => 'user', 'mid' => $mid));
                }
                $this->success(L('measure_data_save_success'), $backurl);
                exit;
            } else {
                $this->error(L('measure_data_save_falure'), url('user/user_measure'));
                exit;
            }
            //}

            $this->success(L('measure_data_save_success'), $backurl);
        } else {
            if(!$measure){
                //读缓存里的数据
                $measureBeginCookie = unserialize(base64_decode(cookie('measureBeginCookie')));
                $this->assign('measureBeginCookie', $measureBeginCookie );
                $m_unit = 1;
                if(in_array($measureBeginCookie['m_unit'],array('ft/in','lbs','ft_in'))){
                    $m_unit = 2;
                }
                $size_value = $this->get_size_value_ajax($measureBeginCookie['height'], $measureBeginCookie['weight'], false);
                $type_status = ($size_value['status'] == 1) ? 1 : 2;
                $this->assign('type_status', $type_status);
                $this->assign('m_unit', $m_unit);
            }
            //尺码对照列表
            $userSizeList = AttributeModel::get_user_size_List();
            $this->assign('userSizeList', $userSizeList);
            $this->assign('measure', isset($measure) ? $measure : array());
            $this->assign('measure_name', isset($measureInfo) ? $measureInfo['measure_name'] : '');
            //判断购物车商品
            $data_arr=array();
            $cartList =  CartModel::get_cart_product_list();
            if($cartList){
                foreach ($cartList['productList'] as &$cart){
                    // 获取商品信息
                    $productInfo = ProductModel::get_product_info($cart['pid']);
                    $model_id=$productInfo['model_id'];
                    if(C("data_".$model_id)){
                        foreach (C("data_".$model_id) as $v){
                            $data_arr[]=$v;
                        }
                    }

                }

            }
            if($cartList['productList']){
                $cart=1;
            }else{
                $cart=0;
            }
            $this->assign('cart',$cart);
            $data_arr= array_unique($data_arr);
            $this->assign('data_arr',$data_arr);
            $this->display('add');
        }
    }

    //检查属于哪个范围（尺码对照）
    public function product_size_ajax()
    {
        $cm = trim($_REQUEST['cm']);
        $kg = trim($_REQUEST['kg']);
        $age = trim($_REQUEST['age']);
        $pid = trim($_REQUEST['pid']);

        $product_user_size = D('product_user_size');

        $where['height'] = array('elt', $cm);
        $where['height2'] = array('egt', $cm);
        $where['weight'] = array('elt', $kg);
        $where['weight2'] = array('egt', $kg);
        $where['age'] = array('elt', $age);
        $where['age2'] = array('egt', $age);

        $product_user_size->where($where);
        $info = $product_user_size->find();

        if ($pid) {
            $product_size = PromotionModel::get_product_size_info($pid);
            $prices = unserialize($product_size['prices']);
            foreach ($prices as $key => $list) {
                $prices_size = $prices[$key];
                if ($prices_size['size']) {
                    $pricesSize = explode("/", $prices_size['size']);

                    if ($pricesSize[0] && $pricesSize[1] && $pricesSize[2]) {
                        $product_user_size02 = D('product_user_size');
                        $product_user_size02->field('circumference,waistline,hipline,shoulder,forepart_length,outside_sleeve');
                        $where02['height'] = array('elt', $pricesSize[0]);
                        $where02['height2'] = array('egt', $pricesSize[0]);
                        $where02['weight'] = array('elt', $pricesSize[1]);
                        $where02['weight2'] = array('egt', $pricesSize[1]);
                        $where02['age'] = array('elt', $pricesSize[2]);
                        $where02['age2'] = array('egt', $pricesSize[2]);

                        $product_user_size02->where($where02);
                        $info02 = $product_user_size02->find();

                        if ($info['circumference'] == $info02['circumference'] && $info['waistline'] == $info02['waistline'] && $info['hipline'] == $info02['hipline'] && $info['shoulder'] == $info02['shoulder'] && $info['forepart_length'] == $info02['forepart_length'] && $info['outside_sleeve'] == $info02['outside_sleeve']) {
                            $chima = $prices_size['size'];
                        }
                    }
                }
            }
        }
        //$aas = $product_user_size->getLastSql();
        $info = array(
            'chima' => $chima,
            'circumference' => $info['circumference'],
            'waistline' => $info['waistline'],
            'hipline' => $info['hipline'],
            'shoulder' => $info['shoulder'],
            'forepart_length' => $info['forepart_length'],
            'outside_sleeve' => $info['outside_sleeve'],
            'id' => $info['id'],
        );

        echo json_encode($info);
        exit;

    }

    /**
     *添加订阅邮件
     */
    public function add_email_subscriptions()
    {
        global $language_id;
        $post = $_POST;
        import('COM.Validate');
        if (!empty($post['email']) && Validate::email($post['email'])) {
            /*import('COM.Validate');
            $validate = new Validate();
            if(!$validate->email($email)){
                $this->error(L('_EMAIL_FORMAT_ERROR_'));
            }*/
            $subscriptions_info = MeasureModel::add_subscriptions_info($post['email']);
            if ($subscriptions_info) {
                if ($post['type'] == 'index') {//来自首页弹出层窗口的订阅
                    cookie('add_email_subscriptions', '1', 'expire=1209600&path=/&domain=' . APP_DOMAIN);
                    $this->success(L('_subscriptions_success_'), url('/product/all'));
                    exit;
                }
                $info = array(
                    'errno' => 1,
                    'errstr' => L('_subscriptions_success_'),
                );
            } else {
                $info = array(
                    'errno' => 0,
                    'errstr' => L('_USER_NAME_ALREADY_EXISTS_'),
                );
            }
        } else {
            $info = array(
                'errno' => 0,
                'errstr' => L('_EMAIL_FORMAT_ERROR_'),
            );
        }

        //
        if($post['is_ajax']){
            echo json_encode($info);exit;
        }else{
            if($info['errno'] == 1)
            $this->success($info['errstr']);
            else
            $this->error($info['errstr']);
        }

    }

    /**
     * 取消订阅邮件
     */
    public function unsubscribe()
    {
        global $language_id;
        $email = base64_decode($_GET['email']);
        $sign = $_GET['sign'];
        if ($sign != MD5($email . C('pwd_key'))) {
            $this->error(L('operation_failed'), url(''));
        }
        MeasureModel::email_subscribe_status($email, 0);

        $data = array(
            'docTitle' => L('unsubscribe_title'),
        );
        $this->assign($data);

        $this->display('user/unsubscribe');
    }

    /**
     * 订阅邮件验证
     */
    public function validate_email_subscribe()
    {
        $email = trim($_REQUEST['email']);
        $email = urldecode($email);
        import('COM.Validate');
        if (empty($email) || !Validate::email($email)) {
            $status = L('_EMAIL_FORMAT_ERROR_');
        } elseif (MeasureModel::email_subscriptions_info($email, 1)) {
            $status = L('_USER_NAME_ALREADY_EXISTS_');
        } else {
            $status = true;
        }
        echo json_encode($status);
        exit;
    }

    public function measure()
    {
        $data = array(
            'docTitle' => L('meta_title_measure'),
            'docKeywords' => L('meta_keywords'),
            'docDescription' => L('meta_description'),
        );
        $this->assign($data);
        $this->assign('robots','noindex,nofollow');
        $this->display('measure');
    }

    //把原先有得身型放到新字段里
    public function measure_pro()
    {
        return;
        $user_measure = D('user_measure')->field('id,measure_data')->order('id asc')->select();
        foreach ($user_measure as $key => $value) {
            $this->measure_pro_update($value);
        }

        echo "结束";
    }

    //把原先有得身型放到新字段里
    public function measure_pro_update($user_measure)
    {

        $measure_data = unserialize($user_measure['measure_data']);
        $data = array();
        //获取身型字段
        $measure_field = MeasureModel::user_measure_field();
        foreach ($measure_field as $value) {
            $field_val = '';
            if ($value['measure_field'] == 'shoulder' || $value['measure_field'] == 'posture') {
                $field_val = $measure_data[$value['measure_field']]['type'];
            } else if ($value['measure_field'] == 'chest_type') {
                $field_val = $measure_data['chest']['type'];
            } else if ($value['measure_field'] == 'stomach_type') {
                $field_val = $measure_data['stomach']['type'];
            } else {
                if (empty($value['field_type'])) {
                    $field_val = $measure_data[$value['measure_field']];
                } else {
                    $field_val = $measure_data[$value['measure_field']][$value['field_type']];
                }
            }
            $data[$value['measure_field']] = $field_val;
        }
        echo 'ID:' . $user_measure['id'] . '处理完成<br />';
        D('user_measure')->where('id=' . $user_measure['id'])->save($data);
    }

    public function take_photos()
    {
        $mid = intval($_GET['mid']);
        if ($mid) {
            $info = M('user_measure')->find($mid);
        } else {
            $cookie_data = unserialize(base64_decode($_COOKIE['measure_data_tmp']));
        }
        if ($info) {
            if ($info['m_unit'] == 2) {
                $ft = $info['height'] / 30.48;
                $in = number_format((($info['height'] / 30.48 - $ft) * 12), 2);
                $size = $ft . 'ft ' . $in . 'in';
            } else {
                $size = $info['height'] . 'cm';
            }
        } else {
            if ($cookie_data['m_unit'] == 2) {
                $ft = intval($cookie_data['measure']['height']['cm'] / 30.48);
                $in = number_format((($cookie_data['measure']['height']['cm'] / 30.48 - $ft) * 12), 2);
                $size = $ft . 'ft ' . $in . 'in';
            } else {
                $size = $cookie_data['measure']['height']['cm'] . 'cm';
            }
        }
        $this->assign('size', $size);
        $this->display('measure/take_photos');
    }

    //判断用户填写的身形名称是否已经填写过，一个用户不能存在两个相同的身形名
    public function check_measure_name_ajax()
    {
        global $user;
        if ($user['id']) {
            $where['user_id'] = $user['id'];
        } else if ($user['sid']) {
            $where['sid'] = $user['sid'];
        }
        $measure_name = $_REQUEST['measure_name'];
        $id = $_REQUEST['measure_id'];

        $measure_list = M('user_measure')->field('measure_name')->where($where)->select();
        if ($id) {
            $measure_info = M('user_measure')->find($id);
            if ($measure_info['measure_name'] == $measure_name) {
                $status = true;
                echo json_encode($status);
                exit;
            }
        }

        foreach ($measure_list as $k => $v) {
            if ($_REQUEST['measure_name'] == $v['measure_name']) {
                $status = L('_MEASURE_NAME_ERROR_');
                echo json_encode($status);
                exit;
            }
        }
        $status = true;
        echo json_encode($status);
        exit;
    }
    //根据身高体重得到上衣尺码
    public function get_size_ajax()
    {

        $height = trim($_POST['measure']['height']['cm']);
        $weight = trim($_POST['measure']['weight']['kg']);
        $height = round($height);
        $weight = round($weight);
        $measureBeginCookie = array(
                'name' => trim($_POST['measure_name']),
                'age' => trim($_POST['measure']['age']),
                'height' => $height,
                'weight' => $weight,
                'm_unit' => trim($_POST['unit']),
        );
        cookie('measureBeginCookie', base64_encode(serialize($measureBeginCookie)));
        if(cookie(measureBeginCookie)){
            echo json_encode(array('status'=>true));
        }else{
            echo json_encode(array('status'=>false));
        }

    }
    //根据身高体重得到上衣尺码
    public function get_size_value_ajax($height = 0, $weight = 0, $is_ajax = true)
    {
        if($_POST['type']){
            $height = trim($_POST['measure']['height']['cm']);
            $weight = trim($_POST['measure']['weight']['kg']);
        }
        $height = round($height);
        $weight = round($weight);

        $db = M('user_size_items');
        if (!empty($height) && !empty($height)) {
            $where['height'] = $height;
            $where['weight'] = $weight;
            $info_1 = $db->where($where)->find();

            if (!$info_1) {
                if ($where['height'] < 160) {//更改身高继续寻找一次
                    $where['height'] += 1;
                }
                if ($where['height'] > 195) {
                    $where['height'] -= 1;
                }
                $info_1 = $db->where($where)->find();
                if (!$info_1) {
                    if ($where['weight'] < 60) {//更改体重继续寻找一次
                        $where['weight'] += 1;
                    }
                    if ($where['weight'] > 90) {
                        $where['weight'] -= 1;
                    }
                    $info_1 = $db->where($where)->find();
                }
            }
            $status = 1;
            if (!$info_1) {
                $status = 3;
            }
        } else {
            $status = 2;
        }
        // var_dump($db->getLastSql());
        //die;
        $info = array(
            'eur_size' => explode(',', $info_1['eur_size']),
            'us_size' => explode(',', $info_1['us_size']),
            'height' => $info_1['height'],
            'weight' => $info_1['weight'],
            'status' => $status,//返回1代表有数据
        );
        //  file_put_contents('ajax.txt', serialize($_POST));
        //保存初始数据到cookie，等待跳转后调用
        if($_POST['type'] == 'add'){
            $measureBeginCookie = array(
                'name' => trim($_POST['measure_name']),
                'age' => trim($_POST['measure']['age']),
                'height' => $height,
                'weight' => $weight,
                'm_unit' => trim($_POST['unit']),
            );
            cookie('measureBeginCookie', base64_encode(serialize($measureBeginCookie)));
        }
        if ($is_ajax) {
            echo json_encode($info);
        } else {
            return $info;
        }
    }

    public function take_photos_index()
    {
        $this->display('measure/take_photos_index');
    }

    public function estimation()
    {
        global $user, $language_id;
        $mid = intval($_REQUEST['mid']);
        if ($mid) {
            $measureInfo = M('user_measure')->find($mid);
            if (!$measureInfo || $measureInfo['user_id'] != $user['id']) {
                $this->error(L('_ERROR_ACTION_'), url('user/user_measure'));
            }
        }
        if (IS_POST) {

            $type = $_GET['type'];
            $measure_data = $_POST['measure'];
            $measure_data2 = $_POST['measure_data2'];
            if (empty($measure_data) || !is_array($measure_data)) {
                $this->error(L('measure data empty'), url('measure/add'));
            }
            if ($type != 'user') {
                $measure_cookie = array(
                    'user_id' => $user['id'],
                    'measure_data' => $measure_data,
                );
                cookie('measure', base64_encode(serialize($measure_cookie)));
            }

            if ($type == 'user') {
                $gourl = url('measure/estimation', array('type' => 'user'));
                $backurl = url('user/user_measure');
            } else if ($type == 'cart') {
                $gourl_arr['type'] = 'cart';
                if (isset($_REQUEST['set']) && $_REQUEST['set'] = 'all') {
                    $gourl_arr['set'] = 'all';
                    //如果已登陆的用户且有身型数据， 是默认有身型数据的
                    if (UserModel::logged() && AttributeModel::get_user_measure_mid()) {
                        redirect(url('cart/info'));
                    }
                }
                $backurl = url('cart/info');
            } else if ($type == 'goto') {
                $gourl = url('measure/estimation', array('type' => 'goto', 'jump_src' => $_GET['jump_src']));
                $jump_src = base64_decode($_GET['jump_src']);
                $backurl = url($jump_src);
            } else if ($type == 'cartshow') {
                $gourl = url('measure/edit', array('type' => 'cartshow', 'mid' => $_GET['mid']));
                $backurl = url('measure/edit', array('type' => 'cartshow', 'mid' => $_GET['mid']));
            } else {
                //$backurl = url('cart/checkout');
                $backurl = url('cart/info');
            }

            if (!UserModel::logged() && !$_GET['countiue_buy']) {
                redirect(url('user/login', array('src' => base64_encode($gourl))));
                exit;
            }
            if ($_GET['countiue_buy']) {
                $backurl = url('cart/checkout', array('countiue_buy' => 1));
            }

            //是否上次照片
            if($_POST['front_photo']){
                if(strstr($_POST['front_photo'],'measure')){
                    $_POST['front_img'] = $_POST['front_photo'];
                }else{
                    $_POST['front_photo'] = substr($_POST['front_photo'], 22);
                    $_POST['front_img'] = MeasureModel::file_put_images($_POST['front_photo'], 'measure/'.date('Y/m/d', time()));
                }
            }
            if($_POST['side_photo']){
                if(strstr($_POST['side_photo'],'measure')){
                    $_POST['side_img'] = $_POST['side_photo'];
                }else{
                    $_POST['side_photo'] = substr($_POST['side_photo'], 22);
                    $_POST['side_img'] = MeasureModel::file_put_images($_POST['side_photo'], 'measure/'.date('Y/m/d', time()));
                }
            }
            if (!$mid) {
                //如果是添加的情况下，需要一起上传
                if(!$_POST['front_photo'] || !$_POST['side_photo']){
                    if($_POST['front_img']) unset($_POST['front_img']);
                    if($_POST['side_img']) unset($_POST['side_img']);
                }
                $mid = MeasureModel::add_measure_data($_POST, 'estimation');
            } else {
                MeasureModel::edit_measure_data($_POST, $mid, 'estimation');
            }
            if ($mid) {
                if ($type == 'cart') {
                    $attribute_list = AttributeModel::get_user_measure_list();
                    if ($cid == 0 || (count($attribute_list) == 1)) {
                        if (isset($user['id'])) {
                            $cart_db_where['user_id'] = $user['id'];
                        } else {
                            $cart_db_where['sid'] = $user['sid'];
                        }
                        $cart_db_where['custom_type'] = 2;
                        $cart_db_where['measure_id'] = array('eq', 0);
                    } else {
                        $cart_db_where = 'id=' . $cid;
                    }
                    $cart_db = D('cart_products');
                    $data['measure_id'] = $mid;
                    $cart_db->where($cart_db_where)->save($data);

                    if (isset($_REQUEST['set']) && $_REQUEST['set'] = 'all') $backurl = url('cart/checkout');
                } else if ($type == 'user') {
                    $backurl = url('measure/edit', array('type' => 'user', 'mid' => $mid));
                }
                $this->success(L('measure_data_save_success'), $backurl);
                exit;
            } else {
                $this->error(L('measure_data_save_falure'), url('user/user_measure'));
                exit;
            }
        }

        if ($mid) {
            //$measure = unserialize($measureInfo['measure_data']);
            //*******************
            //获取身型字段 兼容原先代码
            $measure_field = MeasureModel::user_measure_field();
            foreach ($measure_field as $key => $val) {
                $value = $measureInfo[$val['measure_field']];
                if ($value == 0) {
                    continue;
                }
                if ($val['measure_field'] == 'age') {
                    $new_measure_data[$val['measure_field']] = $value;
                } else if ($val['measure_field'] == 'chest_type') {
                    $new_measure_data['chest'][$val['field_type']] = $value;
                } else if ($val['measure_field'] == 'stomach_type') {
                    $new_measure_data['stomach'][$val['field_type']] = $value;
                } else {
                    $new_measure_data[$val['measure_field']][$val['field_type']] = $value;
                }
            }
            $measure = $new_measure_data;
            $this->assign('edit_type', $_REQUEST['edit_type']);
            $this->assign('measure', $measure);
            $this->assign('measureInfo', $measureInfo);
        }else{
            //读缓存里的数据
            $measureBeginCookie = unserialize(base64_decode(cookie('measureBeginCookie')));
            if($measureBeginCookie){
                $this->assign('measureBeginCookie', $measureBeginCookie );
                //取尺码
                $size_value = $this->get_size_value_ajax($measureBeginCookie['height'], $measureBeginCookie['weight'], false);
                $this->assign('size_value', $size_value );
            }
        }
        $this->display('measure/measurement_estimation');
    }

    function uploadimg(){
        if($_POST['uploadimg']){
            $file = $this->upload('measure/'.date('Y/m/d', time()));
            if(isset($file['img'])){
                $path = $file['img']['savepath'].$file['img']['savename'];
                $path = trim($path,'.');
                $this->assign('imgpath', $path);
            }
        }
        $this->assign('type', $_REQUEST['type']);
        $this->display('measure/uploadimg');
    }

    public function schedule() {
         global $user, $language_id;
        $src=isset($_GET["src"])?unserialize(base64_decode($_GET["src"])):"/schedule";
        if($_POST['dosubmit']){

            //验证token
            if($_POST['ordertoken'] != OrderModel::get_order_token())
            {
                $this->error("不允许提交",'javascript:history.back(-1);');exit;
            }
            $order_measure = M("order_measure");
            $city=C('city_arr');//print_r($area);exit;
            $area=C('area_arr');//print_r($area);exit;
            $post['city_name'] = $city[$_POST['city_id']];;
            if(!$_POST['name']||!$_POST['phone']){
                $this->error('您的信息不完整，请返回完善信息', url("/schedule"));
            }else{
                $post['name']=I('post.name','','htmlspecialchars');
                $post['email']=I('post.email','','htmlspecialchars');
                $post['phone']=I('post.phone','','htmlspecialchars');
                $post['height']=I('post.height','','htmlspecialchars');
                $post['weight']=I('post.weight','','htmlspecialchars');
                $post['address'] = $post['city_name'].$area[$_POST['city_id']][$_POST['area']].I('post.address','','htmlspecialchars');
                $post['remark']=I('post.remark','','htmlspecialchars');
                $post['dt_add']=time();
                $post['class_id']=intval($_POST['class_id']);
                $store_id=$_POST['store_id']?intval($_POST['store_id']):0;
                $post['arrival_time']=strtotime($_POST['arrival_time']);
                if($post['class_id']!=1&&$post['class_id']!=2){
                    $this->error("请选择量体方式！",'javascript:history.back(-1);');exit;
                }
                if($post['class_id']==1&&(!$store_id||!$post['arrival_time'])){
                    $this->error("请选择门店！",'javascript:history.back(-1);');exit;
                }
                if($post['class_id']==2&&!$post['address']){
                    $this->error("请填写地址！",'javascript:history.back(-1);');exit;
                }
                if($post['class_id']==1 && $store_id){
                    if($store_id==1){
                        $post['address']="杭州市下城区长城街22号205";
                    }else if($store_id==2){
                        $post['address']="杭州市西湖区教工路158-2号";
                    }
                }
                $id= $order_measure ->add($post);
                if($id) {
					//生成一条身形数据
					$post2['measure_name'] = $_POST['name'] ;
					$post2['name'] = $_POST['name'];
					$post2['mobile'] = $_POST['phone'];
					$post2['m_status'] = 10;
					$post2['update_time'] = time();
					$post2['order_measure_id'] = $id;
					// $measure_id=MeasureModel::add_measure_data($post2,$measure_mode = 'schel');
					$measure_id = D("user_measure")->add($post2);
					if ($measure_id) {
						//绑定购物车里面的定制商品measure_id
						$cartList = CartModel::get_cart_product_list();//print_r($cartList);exit;
						if ($cartList['productList']) {
							foreach ($cartList['productList'] as &$cart) {
								if ($cart['custom_type'] == 2) {
									//更改measures_id
									D("cart_products")->where(array('id' => $cart['id']))->save(array('measure_id' => $measure_id));
								}
							}
						}
						session("schedule_user_measure_id", $measure_id);
					}
                    $content="";
                    $content.="姓名 ： ".$post['name'];
                    $content.="<br>";
                    $content.="联系电话 ： ".$post['phone'];
                    $content.="<br>";
                    if($post['class_id']==1 && $store_id){
                        $content .= "量体方式 ：到店量体";
                        $content.="<br>";
                        if($store_id==1){
                            $content .= "门      店 ：杭州市下城区长城街22号205";
                        }else if($store_id==2){
                            $content .= "门      店 ：杭州市西湖区教工路158-2号";
                        }
                        $content.="<br>";
                        $content .= "到店时间 ： " .$_POST['arrival_time'];
                    }else{
                        $content .= "量体方式 ： 上门量体";
                        $content.="<br>";
                        $content.="量体地址 ： ".$post['address'];
                    }
                    $content.="<br>";
                    if($_POST['remark']){
                        $content.="备注 ： ".$post['remark'];
                    }
                    $email=array('269897683@qq.com','service@ownonly.net');
                    $mailInstance = new EmailModel();
                    $content = $mailInstance->replaceMail($content);
                    $title = $mailInstance->replaceMail("预约量体");
                    foreach ($email as $v){
                        $mailInstance->sendMail($v, $title, $content);
                    }
                    if($post['class_id']==1) {//到点量体免费
                        $this->success('预约成功', url($src));
                    }else if($post['class_id']==2){//上门量体收费    付款处理  生成一个订单
						$this->success('预约成功', url($src));
                        //获取商品信息
						/*  临时屏蔽   上门量体  不需要收费
                        $info=ProductModel::get_product_info(1713);
                        $post_ordert['user_id']=$user?$user['id']:0;
                        $post_ordert['goods_amount']=$info['custom_price'];
                        $post_ordert['shipping_method']="";
                        $post_ordert['remark']="";
                        $post_ordert['class_id']=1;
                        $post_ordert['shipping_method']="";
                        $post_ordert['payment_method']="Alipay";
                        $post_ordert['delivery_first_name'] = "";
                        $post_ordert['delivery_last_name'] = "";
                        $post_ordert['delivery_cid'] =44;
                        $post_ordert['delivery_pid'] =0;//print_r($post_ordert);exit;
                        if(preg_match("/^1(3[4-9]|47|5[012789]|8[2378])\d{8}$/",$post['phone'])){//移动
                            $post_ordert['delivery_email']  = $post['phone']."@139.com";
                        }else if(preg_match("/^1(3[0-2]|45|5[56]|8[56])\d{8}$/",$post['phone'])){//联通
                            $post_ordert['delivery_email']  = $post['phone']."@wo.com.cn";
                        }else {//电信
                            $post_ordert['delivery_email']  = $post['phone']."@189.cn";
                        }
                        //添加到订单表
                        $order_id = OrderModel::insert_order($post_ordert);
                        if($order_id){
                            //添加产品到订单关联列表
                            $set = array(
                                'order_id' => $order_id,
                                'product_id' => 1713,
                                'product_sn' => $info['sn'],
                                'product_name' => $info['name'],
                                'sell_price' => $info['custom_price'],
                                'qty' => 1,
                                'pay_price' => $info['custom_price'],
                                'total_amount' => $info['custom_price'],
                                'weight'=>0,
                                'custom_type'=>0,
                                'chima'=>"",
                                'car_type'=>0,
                                'designer_id'=>0,
                                'measure_id'=>null,
                                'pants_price'=>0,
                                'pants_qty'=>0
                            );
                            $orderitemid = OrderModel::insert_order_items($order_id, $set);
                            //绑定预约的信息
                            D("order_measure")->where(array('id'=>$id))->save(array('order_id'=>$order_id ));
                            if ($cartList['productList']) {
                                $this->success('预约成功', url("cart/info"));
                            }else {
                                redirect(url('cart/ordersuccee', 'oid=' . $order_id));
                            }
                        }else{
                            D("order_measure")->where(array('id'=>$id))->delete();
                            $this->error("网络错误，请稍后再试！",'javascript:history.back(-1);');exit;
                        }
						*/
                    }
					
				}
			}
        }else{
            $user_measure_count = M("order_measure")->count();
            $user_measure_count = $user_measure_count+35000;
            $this->assign('user_measure_count', $user_measure_count);
            $orderToken = OrderModel::get_order_token();
            $this->assign('ordertoken', $orderToken);
            $this->display("schedule");
        }
    }

    public function promote(){
        global $user, $language_id;
        $src=isset($_GET["src"])?unserialize(base64_decode($_GET["src"])):"/promote";
        if($_POST['dosubmit']){
            //验证token
            if($_POST['ordertoken'] != OrderModel::get_order_token())
            {
                $this->error("不允许提交",'javascript:history.back(-1);');exit;
            }
            $order_measure = M("order_measure");
            $city=C('city_arr');//print_r($area);exit;
            $post['city_name'] = $city[$_POST['city_id']];
            if(!$_POST['name']||!$_POST['phone']){
                $this->error('您的信息不完整，请返回完善信息', url("/promote"));
            }else{
                $area=C('area_arr');//print_r($area);exit;
                $post['name']=I('post.name','','htmlspecialchars');
                $post['email']= isset($_POST['email']) ? I('post.email','','htmlspecialchars') : "";
                $post['phone']=I('post.phone','','htmlspecialchars');
                $post['height'] = isset($_POST['height']) ? I('post.height','','htmlspecialchars') : "";
                $post['weight']= isset($_POST['weight']) ? I('post.weight','','htmlspecialchars') : "";
                $post['address'] = $post['city_name'].$area[$_POST['city_id']][$_POST['area']].I('post.address','','htmlspecialchars');
                $post['remark']= isset($_POST['remark']) ? I('post.remark','','htmlspecialchars') : "";
                $post['dt_add']=time();
                $post['class_id']=intval($_POST['class_id']);
                $post['arrival_time']=strtotime($_POST['arrival_time']);
                $id= $order_measure ->add($post);
                if($id && isset($_POST['height']) && isset($_POST['weight'])) {
                    //生成一条身形数据
                    $post2['measure_name'] = $_POST['name'] ;
                    $post2['name'] = $_POST['name'];
                    $post2['mobile'] = $_POST['phone'];
                    $post2['m_status'] = 10;
                    $post2['update_time'] = time();
                    $post2['order_measure_id'] = $id;
                    $measure_id = D("user_measure")->add($post2);
                    if ($measure_id) {
                        //绑定购物车里面的定制商品measure_id
                        $cartList = CartModel::get_cart_product_list();//print_r($cartList);exit;
                        if ($cartList['productList']) {
                            foreach ($cartList['productList'] as &$cart) {
                                if ($cart['custom_type'] == 2) {
                                    //更改measures_id
                                    D("cart_products")->where(array('id' => $cart['id']))->save(array('measure_id' => $measure_id));
                                }
                            }
                        }
                        session("schedule_user_measure_id", $measure_id);
                    }
                    $content="";
                    $content.="姓名 ：".$post['name'];
                    $content.="<br>";
                    $content.="联系电话 ：".$post['phone'];
                    $content.="<br>";
                    $content.="量体地址 ：".$post['address'];
                    $content.="<br>";
                    if($_POST['remark']){
                        $content.="备注 ：".$post['remark'];

                    }
                    $email=array('269897683@qq.com','service@ownonly.net');
                    $mailInstance = new EmailModel();
                    $content = $mailInstance->replaceMail($content);
                    $title = $mailInstance->replaceMail("预约量体");
                    foreach ($email as $v){
                        $mailInstance->sendMail($v, $title, $content);
                    }
                    if($post['class_id']==1) {//到点量体免费
                        $this->success('预约成功', url($src));
                    }else if($post['class_id']==2){//上门量体收费    付款处理  生成一个订单
                        $this->success('预约成功', url($src));
                    }

                }else{
                    $this->success('预约成功', url($src));
                }
            }
        }else {
            $user_measure_count = M("order_measure")->count();
            $user_measure_count = $user_measure_count + 35000;
            $this->assign('user_measure_count', $user_measure_count);
            $orderToken = OrderModel::get_order_token();
            $this->assign('ordertoken', $orderToken);
            $this->display("promote");
        }
    }
}